﻿using System;
using System.Globalization;
using Hammock;
using Newtonsoft.Json;

namespace OpenNOSClient
{
    public class OpenNOSRestClientBase
    {
        private readonly string key;
        private readonly RestClient client;

        protected RestClient Client
        {
            get { return client; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OpenNOSRestClientBase"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        protected OpenNOSRestClientBase(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("OpenNOS API key is missing");
            }

            // JSON.NET specific serializer implementation
            var settings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Include
            };

            var serializer = new OpenNOSJsonSerializer(settings);

            this.client = new RestClient
            {
                Authority = "http://open.nos.nl",
                VersionPath = "v2",
                Deserializer = serializer,
            };

            //this.client.AddHeader("Accept", "application/json");
            //this.client.AddHeader("Content-Type", "application/json");
            //this.client.UserAgent = "OpenNOSClient";

            this.key = key;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
        protected string GeneratePath(string requestType, OpenNOSCategory category)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}/key/{1}/output/json/category/{2}", requestType, key, category.ToString().ToLowerInvariant());
        }

        protected string GeneratePath(string requestType)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}/key/{1}/output/json/", requestType, key);
        }

        protected string GenerateQueryPath(string query)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}/key/{1}/output/json/q/{2}", OpenNOSRequestTypes.Query, key, query);
        }

        protected string GenerateGuidePath(string guide, DateTime? startDate, DateTime? endDate, string channel)
        {
            string path = this.GeneratePath(guide);

            if (startDate != null && endDate != null)
            {
                path += string.Format(CultureInfo.InvariantCulture, "start/{0}/end/{1}", FormatDate(startDate.Value), FormatDate(endDate.Value));
            }

            if (channel != null)
            {
                path += "/channel/" + channel;
            }

            return path;
        }

        protected static string FormatDate(DateTime date)
        {
            return date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
        }
    }
}
